package com.drug.wms.service.impl;

import com.drug.common.utils.DateUtils;
import com.drug.common.utils.StringUtils;
import com.drug.common.utils.uuid.IdUtils;
import com.drug.wms.domain.*;
import com.drug.wms.mapper.BusOverflowOrderDetailsMapper;
import com.drug.wms.mapper.BusOverflowOrderMapper;
import com.drug.wms.mapper.BusSkuStockMapper;
import com.drug.wms.service.IBusOverflowOrderDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.drug.wms.service.GenerateSequence.genSeqNo;


/**
 * 报损报溢细单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-05
 */
@Service
public class BusOverflowOrderDetailsServiceImpl implements IBusOverflowOrderDetailsService {
    @Autowired
    private BusOverflowOrderDetailsMapper busOverflowOrderDetailsMapper;

    @Autowired
    private BusOverflowOrderMapper busOverflowOrderMapper;


    /**
     * 查询报损报溢细单
     *
     * @param id 报损报溢细单主键
     * @return 报损报溢细单
     */
    @Override
    public BusOverflowOrderAddParam selectBusOverflowOrderDetailsById(String orderId) {
        BusOverflowOrderAddParam busOverflowOrderAddParam = new BusOverflowOrderAddParam();
        BusOverflowOrderDetails busOverflowOrderDetails = new BusOverflowOrderDetails();
        busOverflowOrderDetails.setOrderId(orderId);
        List<BusOverflowOrderDetails> busOverflowOrderDetailsList = busOverflowOrderDetailsMapper.selectBusOverflowOrderDetailsById(orderId);
        BusOverflowOrder busOverflowOrder = busOverflowOrderMapper.selectBusOverflowOrderById(orderId);
        List<BusOverflowOrderDetailsVO> busOverflowOrderDetailsVoList = new ArrayList<>();
        for (BusOverflowOrderDetails overflowOrderDetails : busOverflowOrderDetailsList) {
            BusOverflowOrderDetailsVO vo = new BusOverflowOrderDetailsVO();
            BeanUtils.copyProperties(overflowOrderDetails, vo);
            busOverflowOrderDetailsVoList.add(vo);
        }
        BusOverflowOrderVO busOverflowOrderVO = new BusOverflowOrderVO();
        BeanUtils.copyProperties(busOverflowOrder, busOverflowOrderVO);
        busOverflowOrderAddParam.setBusOverflowOrderDetailsList(busOverflowOrderDetailsVoList);
        busOverflowOrderAddParam.setBusOverflowOrder(busOverflowOrderVO);
        return busOverflowOrderAddParam;
    }

    /**
     * 查询报损报溢细单列表
     *
     * @param busOverflowOrderDetails 报损报溢细单
     * @return 报损报溢细单
     */
    @Override
    public List<BusOverflowOrderDetails> selectBusOverflowOrderDetailsList(BusOverflowOrderDetails busOverflowOrderDetails) {
        return busOverflowOrderDetailsMapper.selectBusOverflowOrderDetailsList(busOverflowOrderDetails);
    }

    /**
     * 新增报损报溢细单
     *
     * @param busOverflowOrderAddParam 报损报溢细单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBusOverflowOrderDetails(BusOverflowOrderAddParam busOverflowOrderAddParam) {
        BusOverflowOrderVO busOverflowOrderVo = busOverflowOrderAddParam.getBusOverflowOrder();
        String orderId = busOverflowOrderVo.getId();
        Integer dataType = busOverflowOrderVo.getDataType();
        BusOverflowOrder busOverflowOrder = new BusOverflowOrder();
        BeanUtils.copyProperties(busOverflowOrderVo, busOverflowOrder);
        if (StringUtils.isEmpty(orderId)) {
            orderId = IdUtils.fastSimpleUUID();
            busOverflowOrder.setId(orderId);
            busOverflowOrder.setEntryDate(new Date());
            String seqNo = genSeqNo(dataType == 0 ? "BS" : "BY" , 10);
            busOverflowOrder.setDocNo(seqNo);
            busOverflowOrder.setValidityCode(0);
            //插入总单
            busOverflowOrderMapper.insertBusOverflowOrder(busOverflowOrder);
        } else {
            busOverflowOrderMapper.updateBusOverflowOrder(busOverflowOrder);
        }
        List<BusOverflowOrderDetailsVO> detailsVoList = busOverflowOrderAddParam.getBusOverflowOrderDetailsList();
        String finalOrderId = orderId;
        detailsVoList.forEach(bean -> {
            BusOverflowOrderDetails busOverflowOrderDetails = new BusOverflowOrderDetails();
            BeanUtils.copyProperties(bean, busOverflowOrderDetails);
            if (StringUtils.isEmpty(bean.getId())) {
                busOverflowOrderDetails.setCreateTime(DateUtils.getNowDate());
                busOverflowOrderDetails.setOrderId(finalOrderId);
                busOverflowOrderDetails.setId(IdUtils.fastSimpleUUID());
                busOverflowOrderDetails.setDataType(busOverflowOrder.getDataType());
                busOverflowOrderDetailsMapper.insertBusOverflowOrderDetails(busOverflowOrderDetails);
            } else {
                busOverflowOrderDetailsMapper.updateBusOverflowOrderDetails(busOverflowOrderDetails);
            }

        });
        return 1;
    }


    /**
     * 修改报损报溢细单
     *
     * @param busOverflowOrderDetails 报损报溢细单
     * @return 结果
     */
    @Override
    public int updateBusOverflowOrderDetails(BusOverflowOrderDetails busOverflowOrderDetails) {
        busOverflowOrderDetails.setUpdateTime(DateUtils.getNowDate());
        return busOverflowOrderDetailsMapper.updateBusOverflowOrderDetails(busOverflowOrderDetails);
    }

    @Override
    public int deleteBusOverflowOrderDetailsByIds(String id) {
        BusOverflowOrderDetails busOverflowOrderDetails = new BusOverflowOrderDetails();
        busOverflowOrderDetails.setId(id);
        busOverflowOrderDetails.setValidityCode("1");
        return busOverflowOrderDetailsMapper.updateBusOverflowOrderDetails(busOverflowOrderDetails);
    }



}
